/*
 * @Author: wuqingchun
 * @Date: 2024-07-02 20:08:25
 * @Description: 软件定时器实现
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-08 16:04:59
 */
#ifndef __ARQ_TIMER_H__
#define __ARQ_TIMER_H__

#include <mutex>
#include <map>
#include <unordered_map>
#include <functional>

typedef struct tagTimerTask
{
    const void* pPrivate;                              // 私有数据
    std::function<void (const void*)> fnCallBack;      // 超时回调函数
    uint64_t ui64Interval;                             // 超时时间间隔，单位为微秒，非0为周期性任务，否则为非周期性任务
    uint64_t ui64Timeout;
    uint32_t uiTaskID;
}TIMER_TASK_S;

class CARQTimer
{
public:
    CARQTimer();
    uint32_t AddTask(uint32_t uiInitUS, uint32_t uiIntervalUS, std::function<void(const void*)> fnCallBack, const void* pPrivate);
    void RemoveTask(std::function<bool (const void*, const void*)> fnCmp, const void* pKey);

    void Run();
private:
    
    std::mutex m_objMtx;
    std::map<uint64_t, TIMER_TASK_S*> m_mapTasks;
    uint32_t m_uiNextTaskId = 0;
};

#endif //__ARQ_TIMER_H__